package com.hua.springCloud.controller;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.*;

@RestController
public class HysitryController {


    @GetMapping("/getInfo/test")
    public String getInfoTest(){
        return "ok";
    }

    @HystrixCommand(fallbackMethod = "getInfo1", commandProperties = {
            //5秒超时算出错
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    @GetMapping("/getInfoTimeOut/test")
    public String getInfoTimeOut(){
//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return "ok";
    }

    public String getInfo1(){
        return "您要访问的服务器崩溃了，请稍后访问~~";
    }

        @RequestMapping("/paymentTest/{id}")
    @HystrixCommand(defaultFallback = "getInfo1",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
    })
    public String getPayCount(@PathVariable("id")int id){
        if(id<0){
            throw new RuntimeException("id不能为负数:"+id);
        }else {
            //糊涂工具包
            String s = IdUtil.simpleUUID();
            return "流水号为："+s;
        }
    }
}
